<?php
//首页显示
namespace newMobile\Controller;
use Think\Controller;
class ApiController extends CommonController {
	
  public function auto_login(){
	$data=I('post.');
	$phone=$data['phone'];
    $password=$data['password'];
    $users=M('users');
    $userinfo=$users->where('phone="'.$phone.'"')->find();
     if($userinfo){
		$pass=passport_decrypt($userinfo['password'],'wnsc');
         if($pass==$password){
         	$res['status']=1;
            $res['info']='账户匹配成功';
		 }else{
             $res['status']=0;
             $res['info']='账户密码不正确';
		 }
     }else{
         $res['status']=2;
         $res['info']='账户不存在';
	 }
	 $this->ajaxReturn($res);
   }

   public  function  gps_dw(){
       $data=I('post.');
       $city_address=$data['city'];
       $lng=$data['lng'];
       $lat=$data['lat'];
       cookie('baidu_lng',$lng,604800);
       cookie('baidu_lat',$lat,604800);
       $city_address='%'.$city_address.'%';
       $user_id=session('user_id');
       if($user_id){
           weizhi($user_id,$data['province'],$data['city2'],$data['district']);
       }

       $citys_map['city_name'] = array('like',$city_address);
       $citys=M('sys_city');
       $rs_d_city=$citys->where($citys_map)->find();
	   if($rs_d_city){
			if(cookie('site_city')==null||cookie('site_city_id')==null){
	           cookie('site_city',$rs_d_city['city_name'],604800);
	           cookie('site_city_id',$rs_d_city['city_id'],604800);
	           $res['status']=1;
	           $res['info']='城市定位成功,请跳转匹配';
	           $res['data']=$rs_d_city;
	       }else{
	           if(cookie('site_city')!=$rs_d_city['city_name']||cookie('site_city_id')!=$rs_d_city['city_id']){
	               $res['status']=0;
	               $res['info']='城市定位成功,询问是否跳转';
	               $res['data']=$rs_d_city;
	            }else{
	               $res['status']=-1;
	               $res['info']='城市定位成功,无需跳转';
	               $res['data']=$rs_d_city;
	           }
	           if(session('no_qiecity')==1){
	               $res['status']=-1;
	               $res['info']='城市定位成功,无需跳转';
	               $res['data']=$rs_d_city;
	           }
	       }
       }else{
       	   $res['status']=-1;
           $res['info']='找不到城市,无需跳转';
           $res['data']=$rs_d_city;
	   }
       $this->ajaxReturn($res);
   }
  public  function  gps_zbaidudw_h5(){
      $data=I('post.');
      $lon=$data['lon'];
      $lat=$data['lat'];
      cookie('baidu_lng',$lon,604800);
      cookie('baidu_lat',$lat,604800);
      $user_id=session('user_id');
      if($user_id){
          weizhi($user_id,$data['province'],$data['city'],$data['district']);
      }

  }
    public  function  gps_dw_h5(){
        $data=I('post.');
        $lon=$data['lon'];
        $lat=$data['lat'];

        $url='http://api.map.baidu.com/geocoder?output=json&location='.$lat.','.$lon.'&key=9skQ8QpKqZOzGHYeAwZt4GquLX5OrhXm';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        if(curl_errno($ch))
        { echo 'CURL ERROR Code: '.curl_errno($ch).', reason: '.curl_error($ch);}
        curl_close($ch);
        $info = json_decode($output, true);

        $city_address=$info['result']['addressComponent']['city'];
        $city_address='%'.$city_address.'%';
        $citys_map['city_name'] = array('like',$city_address);
        $citys=M('sys_city');
        $rs_d_city=$citys->where($citys_map)->find();

        if(cookie('site_city')==null||cookie('site_city_id')==null){
            cookie('site_city',$rs_d_city['city_name']);
            cookie('site_city_id',$rs_d_city['city_id']);
            $res['status']=1;
            $res['info']='城市定位成功,请跳转匹配';
            $res['data']=$rs_d_city;
        }else{
            if(cookie('site_city')!=$rs_d_city['city_name']||cookie('site_city_id')!=$rs_d_city['city_id']){
                $res['status']=0;
                $res['info']='城市定位成功,询问是否跳转';
                $res['data']=$rs_d_city;
            }else{
                $res['status']=-1;
                $res['info']='城市定位成功,无需跳转';
                $res['data']=$rs_d_city;
            }
            if(session('no_qiecity')==1){
                $res['status']=-1;
                $res['info']='城市定位成功,无需跳转';
                $res['data']=$rs_d_city;
            }
        }
     
        $this->ajaxReturn($res);
    }
}
?>
